package com.zy.explore.strategy.infrastructure.strategy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * description 简单工厂类
 *
 * @author 🇨🇳中国印🇨🇳
 * @date 2021年07月03日 02:30
 */
public class CalcStrategyFactory {

    private Map<String, CalcStrategy> map;

    public CalcStrategyFactory() {

        List<CalcStrategy> strategies = new ArrayList<>();

        strategies.add(new NormalVipCalcStrategy());
        strategies.add(new SilverVipCalcStrategy());
        strategies.add(new GoldVipCalcStrategy());
        strategies.add(new PlatinumVipCalcStrategy());

        /* 等同下面
        map = new HashMap<>();
        for (Strategy strategy : strategies) {
            map.put(strategy.getType(), strategy);
        }*/

        // lambda表达式简化代码
        map = strategies.stream().collect(Collectors.toMap(CalcStrategy::getType, strategy -> strategy));
    }

    public static class Holder {
        public static CalcStrategyFactory instance = new CalcStrategyFactory();
    }

    public static CalcStrategyFactory getInstance() {
        return Holder.instance;
    }

    public CalcStrategy get(String type) {
        return map.get(type);
    }
}
